package com.token.iot.bratterycharging.gateway.business;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.gateway.IDGeneration;
import com.token.iot.bratterycharging.gateway.api.PowersFee;
import com.token.iot.bratterycharging.gateway.utils.ByteUtil;


/**
 * 读取功率比例
 * 
 * @author chenzhixuan
 *
 */
@Component("CHODuplexProcessor")
public class CHODuplexProcessor extends DuplexProcessor {

	@Override
	public String request(String imei, Map<String, String> params) throws PendingException {
		String c = "CHO";
		String sessionId = IDGeneration.createSeesionID();
		String command = null;
		command = c + sessionId + "/0";
		loger.info("the send =" + new String(command));
		byte[] cc = ByteUtil.addFlag(command.getBytes());
		context.writeAndFlush(cc);
		return sessionId;

	}

	@Override
	public PowersFee response(String response) throws PendingException {
		String val = response.substring(14);
		String[] params = val.split("#/#");
		loger.info("the CHO response substr =" + val);
		PowersFee pf = new PowersFee();
		pf.setPower1(Integer.parseInt(params[0]));
		pf.setRatio1(Integer.parseInt(params[1]));
		
		pf.setPower2(Integer.parseInt(params[2]));
		pf.setRatio2(Integer.parseInt(params[3]));
		
		pf.setPower3(Integer.parseInt(params[4]));
		pf.setRatio3(Integer.parseInt(params[5]));
		
		pf.setPower4(Integer.parseInt(params[6]));
		pf.setRatio4(Integer.parseInt(params[7]));
		
		pf.setPower5(Integer.parseInt(params[8]));
		pf.setRatio5(Integer.parseInt(params[9]));
		
		
		return pf;
	}

}